<?php

include_once "src/controller/dbmanagement/DBConnection.php";
include_once "src/rrsexception/DBException.php";
include_once "src/rrsexception/DBConnectionException.php";
include_once "src/model/Event.php";
/*
 * This class inserts, updates, deletes, displays, and gets approval for
 * records from the tEvents table
 *
 * @author: Amanda Dunagin
*/

class EventDBManagement {
    /**
     *Precondition:  Accepts an event object:
     *        -User_id must be set
     *Postcondition: Inserts a record into the tEvents table and returns
     *               true if successful and false otherwise.
     **/
    public static function insertEvent($event) {
        try {
            $cnn = new DBConnection();
            $Location = $event->getLocation();
            $Event_Date = $event->getEventDate();
            $Event_Name = $event->getEventName();
            $Description = $event->getDescription();
            $Approval_Indicator = EventDBManagement::boolNumber($event->getApproval());
            $user_Id = $event->getUserId();

            $query = "INSERT INTO tEvents (Location, Event_date,
                                               Event_Name, Description,
                                               Approval_Indicator,
                                               User_Id)
                          VALUES ('$Location', '$Event_Date',
                                  '$Event_Name', '$Description',
                                  '$Approval_Indicator', '$user_Id')";
            $cnn->execute($query);

            //Check if the event was successfully inserted
            $query = "SELECT * FROM tEvents
                          WHERE tEvents.User_Id = '$user_Id'
                          AND tEvents.Event_Name = '$Event_Name'
                          AND tEvents.Location = '$Location'
                          AND tEvents.Event_date = '$Event_Date'";

            $result = $cnn->execute($query);

            $numRows = mysql_numrows($result);

            if ($numRows == 0) {
                $cnn->disconnect();
                return false;
            }
            else {
                $cnn->disconnect();
                return true;
            }
        }
        catch (DBException $e) {
            echo $e;
        }
        catch (DBConnectionException $e) {
            echo $e;
        }
    }

    /**
     *Precondition:  Accepts an event object: The following attributes must be set:
     *                - User_id
     *Postcondition: Deletes a record from the tEvents table and returns
     *               true if successful and false otherwise.
     **/
    public static function deleteEventByID($eventId) {
        try {
            $cnn = new DBConnection();

            $query = "DELETE FROM tEvents WHERE Event_Id = '$eventId'";

            $cnn->execute($query);

            //Check if the record was successfully deleted
            $query = "SELECT * FROM tEvents
                          WHERE tEvents.Event_Id = '$eventId'";

            $result = $cnn->execute($query);

            $numRows = mysql_numrows($result);

            if ($numRows == 0) {
                $cnn->disconnect();
                return true;
            }
            else {
                $cnn->disconnect();
                return false;
            }
            $cnn->disconnect();
        }
        catch (DBException $e) {
            throw $e;
        }
        catch (DBConnectionException $e) {
            throw $e;
        }
    }

    /**
     *Precondition:  Accepts a valid eventId object: The all attributes must be set:
     *
     *Postcondition: updates record in the tEvents table by EventID
     *               No Boolean Value is returned.
     **/
    public static function updateEvent($event) {

        try {
            $cnn = new DBConnection();

            $Event_Id = $event->getEventID();
            $Location = $event->getLocation();
            $Event_Date = $event->getEventDate();
            $Event_Name = $event->getEventName();
            $Approval_Indicator = EventDBManagement::boolNumber($event->getApproval());
            $Description = $event->getDescription();
            $User_Id = $event->getUserId();

            $query = "UPDATE tEvents SET Location = '$Location',
                                             Event_Date = '$Event_Date',
                                             Event_Name = '$Event_Name',
                                             Description = '$Description',
                                             Approval_Indicator = '$Approval_Indicator',
                                             User_id = '$User_Id'
                          WHERE Event_Id = '$Event_Id'";


            $cnn->execute($query);

            $cnn->disconnect();
        }
        catch (DBException $e) {
            throw $e;
        }
        catch (DBConnectionException $e) {
            throw $e;
        }
    }

    /**
     *Precondition:  Accepts an eventId integer: The following must
     *               be valid: - eventId
     *Postcondition: Find and return a specific event in the tEvents table
     *               otherwise return false
     **/
    public static function findEvent($eventId) {
        try {
            $cnn = new DBConnection();
            $eventRecord = new Event();
            $query = "SELECT * FROM tEvents WHERE Event_Id = '$eventId'";
            $result = $cnn->execute($query);

            $numRows = mysql_numrows($result);

            if ($numRows == 0) {
                $cnn->disconnect();
                return false;
            }
            else {
                $row = mysql_fetch_array($result);

                $eventRecord->setEventID($row['Event_Id']);
                $eventRecord->setLocation($row['Location']);
                $eventRecord->setEventDate($row['Event_date']);
                $eventRecord->setEventName($row['Event_Name']);
                $eventRecord->setDescription($row['Description']);
                $eventRecord->setUserId($row['User_id']);
            }

            $cnn->disconnect();
            return $eventRecord;
        }
        catch (DBException $e) {
            throw $e;
        }
        catch (DBConnectionException $e) {
            throw $e;
        }
    }

    /**
     *Precondition:  Accepts an userId
     *
     *Postcondition: Find and return all events in the tEvent table
     *               by a UserID
     **/
    public static function findAllEventsByUserId($userId) {
        try {
            $cnn = new DBConnection();

            $query = "SELECT * FROM tEvents
                          WHERE tEvents.User_id = '$userId'";

            $result = $cnn->execute($query);

            while ($row = mysql_fetch_array($result)) {
                $eventRecord = new Event();
                $eventRecord->setEventID($row['Event_Id']);
                $eventRecord->setLocation($row['Location']);
                $eventRecord->setEventDate($row['Event_date']);
                $eventRecord->setEventName($row['Event_Name']);
                $eventRecord->setDescription($row['Description']);
                $eventRecord->setUserId($row['User_id']);
                $eventArray[] = $eventRecord;
            }

            $cnn->disconnect();
            return $eventArray;
        }
        catch (DBException $e) {
            throw $e;
        }
        catch (DBConnectionException $e) {
            throw $e;
        }
    }

    /**
     *Precondition:  None
     *
     *Postcondition: Find and return all events in the tEvent table
     *
     **/
    public static function findAllEvents() {
        try {
            $cnn = new DBConnection();
            $eventArray = array();
            $query = "SELECT * FROM tEvents";

            $result = $cnn->execute($query);

            while ($row = mysql_fetch_array($result)) {
                $eventRecord = new Event();
                $eventRecord->setEventID($row['Event_Id']);
                $eventRecord->setLocation($row['Location']);
                $eventRecord->setEventDate($row['Event_date']);
                $eventRecord->setEventName($row['Event_Name']);
                $eventRecord->setDescription($row['Description']);
                $eventRecord->setUserId($row['User_id']);
                $eventArray[] = $eventRecord;
            }

            $cnn->disconnect();
            return $eventArray;
        }
        catch (DBException $e) {
            throw $e;
        }
        catch (DBConnectionException $e) {
            throw $e;
        }
    }

     public static function boolNumber($bValue = false) {                      // returns integer
        return ($bValue ? 1 : 0);
    }


}
?>
